package com.collect.zambia.zhiScene.zhiRemind.zhiCustomer.zhiAdmin.zhiRoot;

import android.annotation.SuppressLint;
import android.text.TextUtils;
import android.util.Log;

import androidx.drawerlayout.widget.DrawerLayout;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.collect.zambia.R;
import com.collect.zambia.zhiBase.BaseActivity;
import com.collect.zambia.zhiBean.zhiSend.ZHISCustomerRepayOrderListEntity;
import com.collect.zambia.zhiGlobal.zhiAppSettings.ZHIProjectRouter;
import com.collect.zambia.zhiHelper.ZHIMainProcessHelper;
import com.collect.zambia.zhiHelper.ZHIPhoneCallHelper;
import com.collect.zambia.zhiScene.zhiRemind.zhiCustomer.zhiAdmin.zhiAccount.ZHICustomerAccountAdminFragment;
import com.collect.zambia.zhiScene.zhiRemind.zhiCustomer.zhiAdmin.zhiFollow.ZHICustomerFollowMixAdminFragment;
import com.collect.zambia.zhiScene.zhiRemind.zhiCustomer.zhiAdmin.zhiPerformance.ZHICustomerPerformanceAdminFragment;
import com.collect.zambia.zhiScene.zhiWidget.zhiFilterCustomerService.ZHIFilterCustomerServiceAdmin;
import com.collect.zambia.zhiScene.zhiWidget.zhiFilterDaily.ZHIFilterDailyClickListener;
import com.collect.zambia.zhiScene.zhiWidget.zhiNavigationBar.ZHIBottomTabBarItem;

import butterknife.BindView;

@SuppressLint("NonConstantResourceId")
@Route(path = ZHIProjectRouter.ZHI_ROOT_CUSTOMER_ADMIN)
public class ZHIRootCustomerAdminActivity extends BaseActivity implements ZHIRootCustomerAdminFragmentInterface {

    @BindView(R.id.drawer_layout)
    DrawerLayout drawer_layout;

    @BindView(R.id.drawer_filter_customer_service_admin)
    ZHIFilterCustomerServiceAdmin drawer_filter_customer_service_admin;

    @BindView(R.id.bar_loan)
    ZHIBottomTabBarItem bar_loan;

    @BindView(R.id.bar_marked)
    ZHIBottomTabBarItem bar_marked;

    @BindView(R.id.bar_account)
    ZHIBottomTabBarItem bar_account;

    private FragmentManager mFragmentManager;
    private ZHICustomerFollowMixAdminFragment mZHICustomerFollowMixAdminFragment;
    private ZHICustomerAccountAdminFragment mZHICustomerAccountAdminFragment;
    private ZHICustomerPerformanceAdminFragment mZHICustomerPerformanceAdminFragment;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_root_customer_service_admin;
    }

    @Override
    protected void initInjector() {
//        mActivityComponent.inject(this);
    }

    @Override
    protected void initView() {
        mFragmentManager = getSupportFragmentManager();

        drawer_filter_customer_service_admin.setZHIFilterDailyClickListener(new ZHIFilterDailyClickListener() {
            @Override
            public void onConfirmClick() {
                if (drawer_layout.isDrawerOpen(drawer_filter_customer_service_admin)) {
                    drawer_layout.closeDrawer(drawer_filter_customer_service_admin);
                }
                if (mZHICustomerFollowMixAdminFragment != null) {
                    mZHICustomerFollowMixAdminFragment.refreshData();
                }
            }

            @Override
            public void onCloseClick() {
                if (drawer_layout.isDrawerOpen(drawer_filter_customer_service_admin)) {
                    drawer_layout.closeDrawer(drawer_filter_customer_service_admin);
                }
            }
        });


        initEvent();
        selecFragment(0);

        //初始化电话模块
        ZHIPhoneCallHelper.getInstance().initPhoneCall(getActivity(), ZHIPhoneCallHelper.ZHIPhoneCallType.KFD0);
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.d(TAG, "onResume: ============");
    }

    @Override
    protected void onDestroy() {
        try {
            //移除电话模块
            ZHIPhoneCallHelper.getInstance().stopPhoneCall(getActivity());
        } catch (Exception e) {
            e.printStackTrace();
        }
        super.onDestroy();
    }

    @Override
    public void onShowFilterFollow() {
        if (drawer_layout.isDrawerOpen(drawer_filter_customer_service_admin)) {
            drawer_layout.closeDrawer(drawer_filter_customer_service_admin);
        } else {
            drawer_layout.openDrawer(drawer_filter_customer_service_admin);
        }
    }

    @Override
    public ZHISCustomerRepayOrderListEntity onGetFollowFilterParams() {
        ZHISCustomerRepayOrderListEntity zhisCustomerRepayOrderListEntity = new ZHISCustomerRepayOrderListEntity();
        zhisCustomerRepayOrderListEntity.setType(drawer_filter_customer_service_admin.getOrderType());
        zhisCustomerRepayOrderListEntity.setContactResult(drawer_filter_customer_service_admin.getCallResult());
        zhisCustomerRepayOrderListEntity.setMobile(drawer_filter_customer_service_admin.getUserMobile());
        zhisCustomerRepayOrderListEntity.setSettleFlag(drawer_filter_customer_service_admin.getPaidStatus());

        String member = drawer_filter_customer_service_admin.getMember();
        if (TextUtils.isEmpty(member)) {
            member = ZHIMainProcessHelper.getInstance().getFullName();
        }
        zhisCustomerRepayOrderListEntity.setSysLoginName(member);

        return zhisCustomerRepayOrderListEntity;
    }

    @Override
    public ZHISCustomerRepayOrderListEntity onGetFollowFilterD0Params() {
        ZHISCustomerRepayOrderListEntity zhisCustomerRepayOrderListEntity = new ZHISCustomerRepayOrderListEntity();
        zhisCustomerRepayOrderListEntity.setType(drawer_filter_customer_service_admin.getOrderTypeD0());
        zhisCustomerRepayOrderListEntity.setContactResult(drawer_filter_customer_service_admin.getCallResult());
        zhisCustomerRepayOrderListEntity.setMobile(drawer_filter_customer_service_admin.getUserMobile());
        zhisCustomerRepayOrderListEntity.setSettleFlag(drawer_filter_customer_service_admin.getPaidStatus());

        String member = drawer_filter_customer_service_admin.getMember();
        if (TextUtils.isEmpty(member)) {
            member = ZHIMainProcessHelper.getInstance().getFullName();
        }
        zhisCustomerRepayOrderListEntity.setSysLoginName(member);

        return zhisCustomerRepayOrderListEntity;
    }

    @Override
    public ZHISCustomerRepayOrderListEntity onGetFollowFilterD1Params() {
        ZHISCustomerRepayOrderListEntity zhisCustomerRepayOrderListEntity = new ZHISCustomerRepayOrderListEntity();
        zhisCustomerRepayOrderListEntity.setType(drawer_filter_customer_service_admin.getOrderTypeD1());
        zhisCustomerRepayOrderListEntity.setContactResult(drawer_filter_customer_service_admin.getCallResult());
        zhisCustomerRepayOrderListEntity.setMobile(drawer_filter_customer_service_admin.getUserMobile());
        zhisCustomerRepayOrderListEntity.setSettleFlag(drawer_filter_customer_service_admin.getPaidStatus());

        String member = drawer_filter_customer_service_admin.getMember();
        if (TextUtils.isEmpty(member)) {
            member = ZHIMainProcessHelper.getInstance().getFullName();
        }
        zhisCustomerRepayOrderListEntity.setSysLoginName(member);

        return zhisCustomerRepayOrderListEntity;
    }

    public void initEvent() {
        bar_loan.setCFOnMenuItemDidChangedListener(item -> {
            bar_marked.setChecked(false);
            bar_account.setChecked(false);
            selecFragment(0);
        });
        bar_marked.setCFOnMenuItemDidChangedListener(item -> {
            bar_loan.setChecked(false);
            bar_account.setChecked(false);
            selecFragment(1);
        });
        bar_account.setCFOnMenuItemDidChangedListener(item -> {
            bar_loan.setChecked(false);
            bar_marked.setChecked(false);
            selecFragment(2);
        });
    }

    private void selecFragment(int indext) {
        FragmentTransaction fragemntTransaction = mFragmentManager.beginTransaction();
        hideFragment(fragemntTransaction);
        switch (indext) {
            case 0: {
                bar_loan.setChecked(true);
                bar_marked.setChecked(false);
                bar_account.setChecked(false);
                if (null == mZHICustomerFollowMixAdminFragment) {
                    mZHICustomerFollowMixAdminFragment = new ZHICustomerFollowMixAdminFragment();
                    fragemntTransaction.add(R.id.content, mZHICustomerFollowMixAdminFragment);
                } else {
                    fragemntTransaction.show(mZHICustomerFollowMixAdminFragment);
                }
                break;
            }
            case 1: {
                bar_loan.setChecked(false);
                bar_marked.setChecked(true);
                bar_account.setChecked(false);
                if (null == mZHICustomerPerformanceAdminFragment) {
                    mZHICustomerPerformanceAdminFragment = new ZHICustomerPerformanceAdminFragment();
                    fragemntTransaction.add(R.id.content, mZHICustomerPerformanceAdminFragment);
                } else {
                    fragemntTransaction.show(mZHICustomerPerformanceAdminFragment);
                }
                break;
            }
            case 2: {
                bar_loan.setChecked(false);
                bar_marked.setChecked(false);
                bar_account.setChecked(true);
                if (null == mZHICustomerAccountAdminFragment) {
                    mZHICustomerAccountAdminFragment = new ZHICustomerAccountAdminFragment();
                    fragemntTransaction.add(R.id.content, mZHICustomerAccountAdminFragment);
                } else {
                    fragemntTransaction.show(mZHICustomerAccountAdminFragment);
                }
                break;
            }
            default:
                break;
        }
        fragemntTransaction.commit();
    }

    private void hideFragment(FragmentTransaction fragemntTransaction) {
        if (fragemntTransaction == null) return;
        if (null != mZHICustomerFollowMixAdminFragment) {
            fragemntTransaction.hide(mZHICustomerFollowMixAdminFragment);
        }
        if (null != mZHICustomerAccountAdminFragment) {
            fragemntTransaction.hide(mZHICustomerAccountAdminFragment);
        }
        if (null != mZHICustomerPerformanceAdminFragment) {
            fragemntTransaction.hide(mZHICustomerPerformanceAdminFragment);
        }
    }
}